package com.sansui.blog.service.impl;

import com.sansui.blog.dao.NoticeDao;
import com.sansui.blog.pojo.Notice;
import com.sansui.blog.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @name: NoticeServiceImpl
 * @author: Sansui
 * @Software: IDEA
 * @Blog: https://www.cnblogs.com/sansui6/
 * @date: 2022-04-15 22:32
 **/
@Service
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    private NoticeDao noticeDao;


    /**
     * 获取公告信息
     * @param page
     * @param limit
     * @return
     */
    @Override
    public Map<String, Object> getNoticeList(Integer page, Integer limit) {
        Map<String,Object> data = new HashMap<>(16);
        try{
            Integer pages = (page-1)*limit;
            Integer noticeCount = noticeDao.getNoticeAllCount();
            List<Notice> notices = noticeDao.findAllNotice(pages, limit);
            data.put("code",0);
            data.put("msg","查询成功！");
            data.put("count",noticeCount);
            data.put("data",notices);
        }catch (Exception e){
            e.printStackTrace();
            data.put("code",400);
            data.put("msg","查询失败！");
        }
        return data;
    }


    @Transactional
    @Override
    public Map<String, Object> updateStatus(Long noticeId , String status){
        Map<String,Object> data = new HashMap<>(16);
        try{
            Notice notice = noticeDao.getNoticeByid(noticeId);
            if(notice == null){
                data.put("code",401);
                data.put("msg","修改失败,公告不存在！");
                return data;
            }
            Integer updatenum = noticeDao.updateStatus(noticeId,status);
            if(updatenum != 1){
                data.put("code",402);
                data.put("msg","修改失败！");
                return data;
            }
            data.put("code",200);
            data.put("msg","修改成功！");
        }catch (Exception e){
            e.printStackTrace();
            data.put("code",400);
            data.put("msg","修改失败！");
        }
        return data;
    }


    @Transactional
    @Override
    public Map<String, Object> deleteNotice(Long noticeId){
        Map<String,Object> data = new HashMap<>(16);
        try{
            Notice notice = noticeDao.getNoticeByid(noticeId);
            if(notice == null){
                data.put("code",401);
                data.put("msg","删除失败,公告不存在！");
                return data;
            }
            Integer deletenum = noticeDao.deleteNotice(noticeId);
            if(deletenum != 1){
                data.put("code",402);
                data.put("msg","删除失败！");
                return data;
            }
            data.put("code",200);
            data.put("msg","删除成功！");
        }catch (Exception e){
            e.printStackTrace();
            data.put("code",400);
            data.put("msg","删除失败！");
        }
        return data;
    }


    @Transactional
    @Override
    public Map<String, Object> addNotice(Notice notice,String username){
        Map<String,Object> data = new HashMap<>(16);
        try{
            notice.setCreatetime(new Date());
            notice.setCreateuser(username);
            noticeDao.addNotice(notice);
            data.put("code",200);
            data.put("msg","添加成功！");
        }catch (Exception e){
            e.printStackTrace();
            data.put("code",400);
            data.put("msg","添加失败！");
        }
        return data;
    }


    @Transactional
    @Override
    public Map<String, Object> deleteNoticeByIds(List<String> delList) {
        Map<String,Object> data = new HashMap<>(16);
        try{
            noticeDao.deleteNoticeByIds(delList);
            data.put("code",200);
            data.put("msg","批量删除成功！");
        }catch (Exception e){
            data.put("code",400);
            data.put("msg","批量删除失败！");
        }
        return data;
    }


    @Override
    public Map<String, Object> noticeSearch(String notitle) {
        Map<String,Object> data = new HashMap<>(16);
        try{
            List<Notice> notices = noticeDao.noticeSearch(notitle);
            data.put("code",0);
            data.put("msg","查询成功！");
            data.put("count",notices.size());
            data.put("data",notices);
        }catch (Exception e){
            data.put("code",400);
            data.put("msg","查询失败！");
        }
        return data;
    }


}
